package edu.uoc.tdp.pac4.client.mantenimiento;

import edu.uoc.tdp.pac4.beans.Aula;
import edu.uoc.tdp.pac4.remote.Mantenimiento;
import edu.uoc.tdp.pac4.util.LanguageUtils;
import edu.uoc.tdp.pac4.util.FieldLimit;

import java.rmi.RemoteException;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import java.util.Date;
import java.util.ArrayList;

/**
 *
 * @author eSupport Netbeans
 */
public class PnlMantenimientoAulaGestor extends javax.swing.JDialog 
{
    private Mantenimiento manager;
    private LanguageUtils language;

    private String ActionType;
   
    private Date now                       = new Date();
    private Aula aula                      = null;
    private ArrayList<Aula> aulasinactivas = null;
    private int aulaID;
    
    private int NAME_LENGTH        = 30;
    private int DESCRIPTION_LENGTH = 50;
    private int PLACE_LENGTH       = 50;
   
    /**
      * Creates new form PnlGroupGestor
      */
    public PnlMantenimientoAulaGestor(java.awt.Frame parent, boolean modal, Mantenimiento manager, LanguageUtils language, String ActionType, int aulaID) 
    {
        super(parent, modal);
        initComponents();
      
        setLocationRelativeTo(null);

        this.manager    = manager;
        this.language   = language;
        this.ActionType = ActionType;
        this.aulaID     = aulaID;
      
        addaptToPreferences();

   }

   /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        cmdClose = new javax.swing.JButton();
        fldName = new javax.swing.JTextField();
        lblName = new javax.swing.JLabel();
        lblCapacity = new javax.swing.JLabel();
        lblPlace = new javax.swing.JLabel();
        lblDescript = new javax.swing.JLabel();
        fldCapacity = new javax.swing.JTextField();
        fldPlace = new javax.swing.JTextField();
        cmdAccept = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tarDescript = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jToolBar1.setRollover(true);

        cmdClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/arrow-curve-180.png"))); // NOI18N
        cmdClose.setText("Cerrar");
        cmdClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCloseActionPerformed(evt);
            }
        });

        lblName.setText("Nombre");

        lblCapacity.setText("Capacidad");

        lblPlace.setText("Localización");

        lblDescript.setText("Descripción");

        cmdAccept.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/ok_st_obj.gif"))); // NOI18N
        cmdAccept.setText("Acceptar");
        cmdAccept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAcceptActionPerformed(evt);
            }
        });

        tarDescript.setColumns(20);
        tarDescript.setRows(5);
        jScrollPane1.setViewportView(tarDescript);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(202, Short.MAX_VALUE)
                        .addComponent(cmdAccept)
                        .addGap(45, 45, 45)
                        .addComponent(cmdClose))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblName)
                                    .addComponent(lblPlace)
                                    .addComponent(lblCapacity)
                                    .addComponent(lblDescript))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(fldName, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                                    .addComponent(fldCapacity)
                                    .addComponent(fldPlace))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCapacity)
                    .addComponent(fldCapacity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPlace)
                    .addComponent(fldPlace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDescript)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdClose)
                    .addComponent(cmdAccept))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
   
    private boolean allDataFilled() {
        /*
         * Tanto para añadir  un usuario deberemos tener toda la información.
         * Para modificarlo, toda excepto el password, que es opcional
         * Esta función se encarga de asegurarse de que así sea.
         */
        if (fldName.getText().isEmpty()     || fldName.getText().equals(""))     {return false;}
        if (fldCapacity.getText().isEmpty() || fldCapacity.getText().equals("")) {return false;}
        if (fldPlace.getText().isEmpty()    || fldPlace.getText().equals(""))    {return false;}
        //if (tarDescript.getText().isEmpty() || tarDescript.getText().equals("")) {return false;}
        
        return true;
    }
    private void setLabelsLanguage() {
        /*
         * Definimos el texto de las labels del panel en función del idioma seleccionado
         */
        lblName.setText     (language.getProperty("mantenimiento.usermain.name"));
        lblCapacity.setText (language.getProperty("mantenimiento.aulasmain.capacidad"));
        lblPlace.setText    (language.getProperty("mantenimiento.aulasmain.localizacion"));
        lblDescript.setText (language.getProperty("mantenimiento.aulasmain.descripcion"));      
       
        if (this.ActionType.equalsIgnoreCase("Add")) {
            this.cmdAccept.setText(language.getProperty("mantenimiento.usermain.newUser"));
        }
        else if (this.ActionType.equalsIgnoreCase("Edit")){
            this.cmdAccept.setText(language.getProperty("mantenimiento.usermain.modUser"));
        }
        this.cmdClose.setText(language.getProperty("mantenimiento.usermain.back"));
    }
   
    private void addaptToPreferences() {
        /*
         * El mismo panel se usa para añadir/modificar usuarios, hay que adaptar ciertas características
         * en función de qué se solicite
         */
        this.setLabelsLanguage();
        
        this.fldName.setDocument(new FieldLimit(NAME_LENGTH));
        this.fldPlace.setDocument(new FieldLimit(PLACE_LENGTH));
        this.tarDescript.setDocument(new FieldLimit(DESCRIPTION_LENGTH));
                
        // Cambios Vinculados al tipo de llamada
        if (this.ActionType.equalsIgnoreCase("Add")) {
            this.addaptToAddAula();
        } else if (this.ActionType.equalsIgnoreCase("Edit")) {
            this.addaptToEditAula();
        } else if (this.ActionType.equalsIgnoreCase("Explore")) {
            this.addaptToExploreAula();
        }
    }
   
    private void addaptToAddAula(){
        
        this.setTitle(language.getProperty("mantenimiento.main.title") + ". " + 
                      language.getProperty("mantenimiento.main.aula")  + ". " +
                      language.getProperty("mantenimiento.usermain.newUser"));
       
       try {
            this.aulasinactivas = manager.getAulasInactivas();
       }
       catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, 
                                       language.getProperty("err.sql") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                                       language.getProperty("app.title"), 
                                       JOptionPane.ERROR_MESSAGE);
         
        } 
        catch (Exception ex) {
                JOptionPane.showMessageDialog(null, 
                                       language.getProperty("err.generic") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                                       language.getProperty("app.title"), 
                                       JOptionPane.ERROR_MESSAGE);
        }
       
       /*
        * Cuando vamos a añadir un nuevo usuario los parametros (excepto Fecha de Registro)
        * se inicializan en blanco
        * 
        * El usuario de creación por defecto es Alumno, que debería ser el más abundante
        */
       fldName.setText     ("");
       fldCapacity.setText ("");
       fldPlace.setText    ("");
       tarDescript.setText ("");
            
       this.cmdAccept.setText(language.getProperty("mantenimiento.usermain.newUser"));
    }
   
    private void addaptToEditAula(){
        
        this.setTitle(language.getProperty("mantenimiento.main.title") + ". " + 
                      language.getProperty("mantenimiento.main.user")   + ". " +
                      language.getProperty("mantenimiento.usermain.modUser"));
       
       /*
        * Cuando vamos a modificar un usuario, inicializamos los formularios con los valores
        * que tiene el usuario
        */
        try {
            this.aula = manager.getAula(this.aulaID);
            
            this.fldName.setText     (aula.getNombre());
            this.fldCapacity.setText ("" + aula.getCapacidad());
            this.fldPlace.setText    (aula.getLocalizacion());
            this.tarDescript.setText (aula.getDescripcion());
        }
        catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, 
                                       language.getProperty("err.sql") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                                       language.getProperty("app.title"), 
                                       JOptionPane.ERROR_MESSAGE);
         
        } 
        catch (Exception ex) {
                JOptionPane.showMessageDialog(null, 
                                       language.getProperty("err.rmi") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                                       language.getProperty("app.title"), 
                                       JOptionPane.ERROR_MESSAGE);

        }
           
        this.cmdAccept.setText(language.getProperty("mantenimiento.usermain.modUser")); 
    }
    
    private void addaptToExploreAula() {
        this.addaptToEditAula();
        this.fldName.setEditable(false);
        this.fldCapacity.setEditable(false);
        this.fldPlace.setEditable(false);
        this.tarDescript.setEditable(false);
        this.cmdAccept.setVisible(false);
    }
   
   private void cmdCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCloseActionPerformed

    /*
     * Cerramos el formulario
     */
    this.dispose();

   }//GEN-LAST:event_cmdCloseActionPerformed

    private void cmdAddAulaAction() {
        /*
         * Gestión de Añadir Nuevo Usuario
         * Creamos una instancia Usuario con toda la info recibida y luego la pasamos al manager
         * para que la incluya en la BD
         * 
         * Si añadimos correctamente se cierra el panel
         */
        try {
            this.aula = new Aula();
            
            aula.setNombre(this.fldName.getText());
            aula.setCapacidad(new Integer(this.fldCapacity.getText()));
            aula.setLocalizacion(this.fldPlace.getText());
            aula.setDescripcion(this.tarDescript.getText());
            
            aula.setFechaAlta(this.now);
            aula.setActiva(false);
            aula.setFechaInactividad(this.now);
            
            for (Aula oldaula : this.aulasinactivas) {
                if (aula.compare(oldaula)) {
                    if (manager.undeleteAula(oldaula.getId())) {
                        JOptionPane.showMessageDialog(null, language.getProperty("mantenimiento.msg.add.oldaula"),
                                              "Información", JOptionPane.INFORMATION_MESSAGE);
                        this.dispose();
                        return;
                    }
                    break;
                }
            }
                    
            if (manager.addAula(aula)) {
                JOptionPane.showMessageDialog(null, language.getProperty("mantenimiento.msg.add.aula"),
                                              "Información", JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
            }
        }
        catch (SQLException ex) {
                 JOptionPane.showMessageDialog(null, 
                                               language.getProperty("err.sql") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                                               language.getProperty("app.title"), 
                                               JOptionPane.ERROR_MESSAGE);
        }
        catch (Exception ex) {
                 JOptionPane.showMessageDialog(null, 
                                               language.getProperty("err.generic") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                                               language.getProperty("app.title"), 
                                               JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void cmdModAulaAction() {
        /*
         * Gestión de odificar Usuario
         * Creamos una instancia Usuario con toda la info recibida y luego la pasamos al manager
         * para que la actualize la BD (no se accede al UserID)
         * 
         * Para la modificacion solicitaremos confirmación
         * 
         * Si modificamos correctamente se cierra el panel
         */
        try {
            
            Aula new_aula = new Aula();
            
            new_aula.setId(this.aulaID);
            new_aula.setNombre(this.fldName.getText());
            new_aula.setCapacidad(new Integer(this.fldCapacity.getText()));
            new_aula.setLocalizacion(this.fldPlace.getText());
            new_aula.setDescripcion(this.tarDescript.getText());
            
            new_aula.setFechaAlta(this.aula.getFechaAlta());
            new_aula.setActiva(this.aula.isActiva());
            
            if (this.aula.getFechaInactividad() != null) {
                new_aula.setFechaInactividad (this.aula.getFechaInactividad());
            }
            else {
                new_aula.setFechaInactividad (this.aula.getFechaAlta());
            }
            
            
            Object[] options = {language.getProperty("opt.si"), language.getProperty("opt.no")};
            int reply = JOptionPane.showOptionDialog(this, language.getProperty("mantenimiento.msg.confirm"), language.getProperty("app.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, now);
            if (reply == 0) {
                if (manager.updateAula(new_aula)) {
                    JOptionPane.showMessageDialog(null, language.getProperty("mantenimiento.msg.modif"),
                                                  "Información", JOptionPane.INFORMATION_MESSAGE);
                }
            }
            this.dispose();
        }
        catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, 
                                          language.getProperty("err.sql") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                                          language.getProperty("app.title"), 
                                          JOptionPane.ERROR_MESSAGE);
        }
        catch (Exception ex) {
            JOptionPane.showMessageDialog(null, 
                                          language.getProperty("err.rmi") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                                          language.getProperty("app.title"), 
                                          JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void cmdAcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAcceptActionPerformed
        /*
         * Las acciones sólo se llevaran a cabo si tenemos TODOS los campos llenos
         */
        if (!this.allDataFilled()) {
            JOptionPane.showMessageDialog(null, 
                                          language.getProperty("mantenimiento.err.fields"), 
                                          language.getProperty("app.title"), 
                                          JOptionPane.ERROR_MESSAGE);
        } else {
            if (this.ActionType.equalsIgnoreCase("Add")) {
                this.cmdAddAulaAction();
            }
            else if (this.ActionType.equalsIgnoreCase("Edit")) {
                this.cmdModAulaAction();
            }
        }
    }//GEN-LAST:event_cmdAcceptActionPerformed
   
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdAccept;
    private javax.swing.JButton cmdClose;
    private javax.swing.JTextField fldCapacity;
    private javax.swing.JTextField fldName;
    private javax.swing.JTextField fldPlace;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblCapacity;
    private javax.swing.JLabel lblDescript;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPlace;
    private javax.swing.JTextArea tarDescript;
    // End of variables declaration//GEN-END:variables
}
